from flask import session, current_app, jsonify, g

from info.utils.response_code import RET


# 新闻排行的装饰器
def index_filter(index):
    if index == 0:
        return 'first'
    elif index == 1:
        return 'second'
    elif index == 2:
        return 'third'
    else:
        return ''


# 用户登陆验证
import functools


def user_login_data(f):
    @functools.wraps(f)
    def wrapper(*args, **kwargs):
        user_id = session.get('user_id')
        from info.models import User
        try:
            user = User.query.filter(user_id == User.id).first()
        except Exception as e:
            current_app.logger.error(e)
            return jsonify(errno=RET.DBERR, errmsg='用户信息查询失败')
        g.user = user
        return f(*args, **kwargs)
    return wrapper
